﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RSALicCore
{
    /// <summary>
    /// 授权管理器
    /// </summary>
    public class LicManager
    {
        static string LicFileName = "License";

        static string GetLicPath()
        {
            return Path.Combine(Application.StartupPath, LicFileName);
        }

        /// <summary>
        /// 保存授权数据
        /// </summary>
        /// <param name="pubKey"></param>
        /// <param name="lic"></param>
        public static void Save(string lic)
        {
            using(var sw = new StreamWriter(GetLicPath()))
            {
                sw.WriteLine("此文件为本机的授权文件，请勿修改或删除，否者需要再次授权");
                sw.WriteLine(lic);
            }
        }

        /// <summary>
        /// 读取授权数据
        /// </summary>
        /// <returns></returns>
        public static string Read()
        {
            var licPath = GetLicPath();
            if (!File.Exists(licPath))
            {
                return "";
            }

            try
            {
                using (var sr = new StreamReader(licPath))
                {
                    sr.ReadLine();
                    return sr.ReadLine();
                }
            }
            catch (Exception)
            {
                return "";
            }
        }
    }
}
